var content = document.querySelector('.content');
var tbody = document.querySelector(".grade-table tbody");
var back = document.getElementById('back');
var exportBtn = document.getElementById('toExcel');

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data = [];

document.addEventListener("DOMContentLoaded", function() {

  $.ajax({
    url: '/management/stuservlet/schedule',
    method: 'POST',
    data: {
      username: "1"
    },
    success: function(response) {
      console.log(response);
      console.log(response.data);
      var data = response.data;
      // 获取表格的 tbody 元素
      var tbody = document.querySelector('.grade-table tbody');
      // 遍历返回的数据
      data.forEach(function(item) {
        // 根据时间段找到对应的行
        var row = tbody.querySelector('tr:nth-child(' + item.Cou_class + ')');

        // 找到对应的单元格并填充数据
        var cell = row.querySelector('td:nth-child(' + (item.Cou_zhou + 1) + ')');
        cell.textContent = item.Cou_name;
      });
    }
  });

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }
});

exportBtn.addEventListener('click', function() {
  var table = document.querySelector('.grade-table');
  // 创建一个新的 Workbook 对象
  var wb = XLSX.utils.book_new();
  // 将表格转换为一个 Worksheet 对象，并传递一个配置对象
  var ws = XLSX.utils.table_to_sheet(table, {cellStyles: true});
  // 将 Worksheet 对象添加到 Workbook 对象中
  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
  // 生成 Excel 文件并下载
  XLSX.writeFile(wb, 'schedule.xlsx');
});


back.addEventListener('click', function() {
  window.history.back();
});